﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ADayRoiAdmin
{
    public partial class fmImages : Form
    {
        int product_id;
        SqlConnection con = null;
        AdayroiClass adayroi = null;
        string colName = "", currentImage="";
      
        private BindingManagerBase bm = null;
        private string temp_picThumb = string.Empty, temp_picSummary = string.Empty, temp_picList = string.Empty, 
            temp_picSmall = string.Empty, temp_picLarge = string.Empty;
           string folder = "PersonalSales";
        
        public fmImages(int _productid, string colName)
        {
            this.product_id = _productid;
            this.colName = colName;
            adayroi = new AdayroiClass(ref con);
            if (colName == "ProductID")
                folder = "Products";
            InitializeComponent();
        }

        private void fmImages_Load(object sender, EventArgs e)
        {
            DataTable dt_temp_image = adayroi.getList("select "+colName+", ImageList from Images where " + colName + " = " + product_id, con);
            DataTable dt_image = dt_temp_image.Clone();
            DataRow dr_image = null;
            if (dt_temp_image.Rows.Count > 0)
            {
                if (dt_temp_image.Rows[0]["ImageList"].ToString() != "")
                {
                    string[] str_ImageList = dt_temp_image.Rows[0]["ImageList"].ToString().Split('@');
                    foreach (string s in str_ImageList)
                    {
                        dr_image = dt_image.NewRow();
                        dr_image[colName] = product_id;
                        dr_image["ImageList"] = s;
                        dt_image.Rows.Add(dr_image);
                    }
                }

                DataTable dt_Small_Large = adayroi.getList("select ImageLarge,ImageSmall from Images where " + colName + " = " + product_id, con);
                if (dt_Small_Large.Rows[0]["ImageSmall"].ToString() != "")
                    adayroi.lay_hinh("/Code/Upload/" + folder + "/SmallImage/" + dt_Small_Large.Rows[0]["ImageSmall"].ToString(), pic_Small);
                if (dt_Small_Large.Rows[0]["ImageLarge"].ToString() != "")
                    adayroi.lay_hinh("/Code/Upload/" + folder + "/SmallImage/" + dt_Small_Large.Rows[0]["ImageLarge"].ToString(), pic_Large);
            }

            dgv_Images.DataSource = dt_image.DefaultView;
            bm = dgv_Images.BindingContext[dgv_Images.DataSource, dgv_Images.DataMember];
        }

        private void dgv_Images_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
         
            if (findRow["ImageList"].ToString() != string.Empty)
            {
                currentImage=findRow["ImageList"].ToString();
                adayroi.lay_hinh("/Code/Upload/" + folder + "/ListImage/" + currentImage , pic_List);
            }
           
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            DataTable dt_price = adayroi.getList("select * from Images where " + colName + "=" + product_id, con);
            int rowcount = dt_price.Rows.Count;
            string folder = "PersonalSales";
            if (colName == "ProductID")
                folder = "Products";

            if(rowcount>0)
            {
                if (!String.IsNullOrEmpty(temp_picList) && Path.GetFileName(temp_picList) != dt_price.Rows[0]["ImageList"].ToString())
                    adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/ListImage/", dt_price.Rows[0]["ImageList"].ToString());

                if (!String.IsNullOrEmpty(temp_picSmall) && Path.GetFileName(temp_picSmall) != dt_price.Rows[0]["ImageSmall"].ToString())
                    adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/SmallImage/", dt_price.Rows[0]["ImageSmall"].ToString());

                if (!String.IsNullOrEmpty(temp_picLarge) && Path.GetFileName(temp_picLarge) != dt_price.Rows[0]["ImageLarge"].ToString())
                    adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/LargeImage/", dt_price.Rows[0]["ImageLarge"].ToString());
            }

            if (!String.IsNullOrEmpty(temp_picList))
            if (adayroi.ftp_picture(temp_picList, "/Code/Upload/" + folder + "/ListImage/" + Path.GetFileName(temp_picList)) == 0)
            {
                string str_pic = Path.GetFileName(temp_picList);

                string sql = "";
                if (rowcount == 0)
                {
                    sql = " Insert into Images (PersonalSalesID,ImageList) values (" + product_id + ",'" + str_pic + "')";
                    rowcount = 1;
                }
                else
                {
                    if (dt_price.Rows[0]["ImageList"].ToString() != "")
                    {
                        str_pic += "@" + dt_price.Rows[0]["ImageList"].ToString();
                        if (currentImage != "")
                            str_pic.Replace(currentImage, Path.GetFileName(temp_picList));
                    }
                    sql = "Update Images set ImageList = '" + str_pic
                       + "' where " + colName + " = " + product_id;
                }
                MessageBox.Show(sql);
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                
            }

            if(!String.IsNullOrEmpty(temp_picSmall))
                if (adayroi.ftp_picture(temp_picSmall, "/Code/Upload/" + folder + "/SmallImage/" + Path.GetFileName(temp_picSmall)) == 0)
                {
                    string str_pic2 = Path.GetFileName(temp_picSmall);
                    string sql2 = "";
                    if (rowcount == 0)
                    {
                        sql2 = "Insert into Images(PersonalSalesID,ImageSmall) values (" + product_id + ",'" + str_pic2 + "')";
                        rowcount = 1;
                    }
                    else
                    {
                       
                        sql2 = "Update Images set ImageSmall='" + str_pic2 + "' where " + colName + "=" + product_id;
                    }
                    if (adayroi.UpdateSQL(con, sql2) == 0)
                        MessageBox.Show("Thành Công !...");
                }

            if (!String.IsNullOrEmpty(temp_picLarge))
                if (adayroi.ftp_picture(temp_picLarge, "/Code/Upload/" + folder + "/SmallImage/" + Path.GetFileName(temp_picLarge)) == 0)
                {
                    string str_pic3 = Path.GetFileName(temp_picLarge);
                    string sql3 = "";
                    if (rowcount == 0)
                    {
                        sql3 = "Insert into Images(PersonalSalesID,ImageLarge) values (" + product_id + ",'" + str_pic3 + "')";
                        rowcount = 1;
                    }
                    else
                    {
                       sql3 = "Update Images set ImageLarge='" + str_pic3 + "' where " + colName + "=" + product_id;
                    }
                    if (adayroi.UpdateSQL(con, sql3) == 0)
                        MessageBox.Show("Thành Công !...");
                }


                else
                {
                    MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
                }

           // string str = "2.jpg@1.jpg@";

           // if (str.EndsWith("@"))
           //     str.Remove(str.Length - 2, 1);
          
        }

        private void pic_List_Click(object sender, EventArgs e)
        {
            string picture = adayroi.get_path_pic();
            temp_picList = adayroi.loadpicture(picture, pic_List);
        }
       
        private void pic_Small_Click(object sender, EventArgs e)
        {
            string pic2 = adayroi.get_path_pic();
            temp_picSmall = adayroi.loadpicture(pic2, pic_Small);
        } 
        private void pic_Large_Click(object sender, EventArgs e)
        {
            string Pic3 = adayroi.get_path_pic();
            temp_picLarge = adayroi.loadpicture(Pic3, pic_Large);
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {

           // DataRow findRow = ((DataRowView)bm.Current).Row;
            //if (adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Links/", findRow["Images"].ToString()) == 0)
            //{
            //    string sql = "delete from ImagesLinks where PKID = " + findRow["PKID"].ToString();
            //    if (adayroi.UpdateSQL(con, sql) == 0)
            //        MessageBox.Show("Thành Công");
               
            //}
            //else
            //{
            //    MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
            //}

            DataRow findRow = ((DataRowView)bm.Current).Row;
            string folder = "PersonalSales";
            if (colName == "ProductID")
               folder = "Products";

            if (adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/" + folder + "/ListImage/", findRow["ImageList"].ToString()) == 0)
            {
                string sql="Delete from ImageList where PKID="+ findRow["PKID"].ToString();

            }
       
        }
    }
}
